package com.gun.management.module.task.controller.admin.taskRewardRecord;

import org.springframework.web.bind.annotation.*;
import jakarta.annotation.Resource;
import org.springframework.validation.annotation.Validated;
import org.springframework.security.access.prepost.PreAuthorize;
import io.swagger.v3.oas.annotations.tags.Tag;
import io.swagger.v3.oas.annotations.Parameter;
import io.swagger.v3.oas.annotations.Operation;

import jakarta.validation.Valid;
import jakarta.servlet.http.HttpServletResponse;
import java.util.*;
import java.io.IOException;

import com.gun.management.framework.common.pojo.PageParam;
import com.gun.management.framework.common.pojo.PageResult;
import com.gun.management.framework.common.pojo.CommonResult;
import com.gun.management.framework.common.util.object.BeanUtils;
import static com.gun.management.framework.common.pojo.CommonResult.success;

import com.gun.management.framework.excel.core.util.ExcelUtils;

import com.gun.management.framework.apilog.core.annotation.ApiAccessLog;
import static com.gun.management.framework.apilog.core.enums.OperateTypeEnum.*;

import com.gun.management.module.task.controller.admin.taskRewardRecord.vo.*;
import com.gun.management.module.task.dal.dataobject.taskRewardRecord.TaskRewardRecordDO;
import com.gun.management.module.task.service.taskRewardRecord.TaskRewardRecordService;

@Tag(name = "管理后台 - 任务奖励记录信息")
@RestController
@RequestMapping("/task/reward-record")
@Validated
public class TaskRewardRecordController {

    @Resource
    private TaskRewardRecordService rewardRecordService;

    @PostMapping("/create")
    @Operation(summary = "创建任务奖励记录信息")
    @PreAuthorize("@ss.hasPermission('task:reward-record:create')")
    public CommonResult<Long> createRewardRecord(@Valid @RequestBody TaskRewardRecordSaveReqVO createReqVO) {
        return success(rewardRecordService.createRewardRecord(createReqVO));
    }

    @PutMapping("/update")
    @Operation(summary = "更新任务奖励记录信息")
    @PreAuthorize("@ss.hasPermission('task:reward-record:update')")
    public CommonResult<Boolean> updateRewardRecord(@Valid @RequestBody TaskRewardRecordSaveReqVO updateReqVO) {
        rewardRecordService.updateRewardRecord(updateReqVO);
        return success(true);
    }

    @DeleteMapping("/delete")
    @Operation(summary = "删除任务奖励记录信息")
    @Parameter(name = "id", description = "编号", required = true)
    @PreAuthorize("@ss.hasPermission('task:reward-record:delete')")
    public CommonResult<Boolean> deleteRewardRecord(@RequestParam("id") Long id) {
        rewardRecordService.deleteRewardRecord(id);
        return success(true);
    }

    @GetMapping("/get")
    @Operation(summary = "获得任务奖励记录信息")
    @Parameter(name = "id", description = "编号", required = true, example = "1024")
    @PreAuthorize("@ss.hasPermission('task:reward-record:query')")
    public CommonResult<TaskRewardRecordRespVO> getRewardRecord(@RequestParam("id") Long id) {
        return success(BeanUtils.toBean(rewardRecordService.getTaskRewardRecordById(id), TaskRewardRecordRespVO.class));
    }

    @GetMapping("/page")
    @Operation(summary = "获得任务奖励记录信息分页")
    @PreAuthorize("@ss.hasPermission('task:reward-record:page')")
    public CommonResult<PageResult<TaskRewardRecordRespVO>> getRewardRecordPage(@Valid TaskRewardRecordPageReqVO pageReqVO) {
        PageResult<TaskRewardRecordRespVO> pageResult = rewardRecordService.getRewardRecordPage(pageReqVO);
        return success(BeanUtils.toBean(pageResult, TaskRewardRecordRespVO.class));
    }

    @GetMapping("/export-excel")
    @Operation(summary = "导出任务奖励记录信息 Excel")
    @PreAuthorize("@ss.hasPermission('task:reward-record:export')")
    @ApiAccessLog(operateType = EXPORT)
    public void exportRewardRecordExcel(@Valid TaskRewardRecordPageReqVO pageReqVO,
              HttpServletResponse response) throws IOException {
        pageReqVO.setPageSize(PageParam.PAGE_SIZE_NONE);
        List<TaskRewardRecordRespVO> list = rewardRecordService.getRewardRecordPage(pageReqVO).getList();
        // 导出 Excel
        ExcelUtils.write(response, "任务奖励记录信息.xls", "数据", TaskRewardRecordRespVO.class,
                        BeanUtils.toBean(list, TaskRewardRecordRespVO.class));
    }

}